
// alert("123123");
// alert("qqqqqqqqqq");
// alert("ffffffffff");



// 获取输入框和建议列表元素
const searchInput = document.getElementById('input_search');
const suggestionsList = document.getElementById('suggestions');

let selectedIndex = -1; // 当前选中建议的索引

// 监听输入框的输入事件
searchInput.addEventListener('input', () => {
    const inputValue = searchInput.value.trim();
    suggestionsList.innerHTML = '';
    selectedIndex = -1;

    if (inputValue !== '') {
        const filteredSuggestions = getSuggestions(inputValue);

        filteredSuggestions.forEach((suggestion, index) => {
            const li = document.createElement('li');
            li.textContent = suggestion;
            suggestionsList.appendChild(li);

            li.addEventListener('click', () => {
                selectSuggestion(index);
            });
        });
    }
});

// 根据输入值获取匹配的建议结果
function getSuggestions(input) {
    return suggestionsData.filter((suggestion) =>
        suggestion.toLowerCase().startsWith(input.toLowerCase())
    );
}

// 监听键盘按键事件
searchInput.addEventListener('keydown', function(event) {
    if (event.key === 'Tab' && suggestionsList.children.length > 0) {
        searchInput.value = suggestionsList.children[0].textContent;
        suggestionsList.innerHTML = '';
        event.preventDefault();
    }
});

// 监听键盘按键事件
searchInput.addEventListener('keydown', (event) => {
    const suggestions = suggestionsList.getElementsByTagName('li');

    if (event.key === 'ArrowUp') {
        event.preventDefault();
        if (selectedIndex > 0) {
            selectedIndex--;
            updateSelectedSuggestion(suggestions);
        }
    } else if (event.key === 'ArrowDown') {
        event.preventDefault();
        if (selectedIndex < suggestions.length - 1) {
            selectedIndex++;
            updateSelectedSuggestion(suggestions);
        }
    } else if (event.key === 'Enter' && selectedIndex !== -1) {
        event.preventDefault();
        selectSuggestion(selectedIndex);
    }
});



// 更新选中项的样式
function updateSelectedSuggestion(suggestions) {
    for (let i = 0; i < suggestions.length; i++) {
        const suggestion = suggestions[i];
        if (i === selectedIndex) {
            suggestion.classList.add('selected');
            searchInput.value = suggestion.innerHTML;
        } else {
            suggestion.classList.remove('selected');
        }
    }
}

// 选择建议并填充到输入框
function selectSuggestion(index) {
    const selectedSuggestion = suggestionsList.getElementsByTagName('li')[index];
    if (selectedSuggestion) {
        searchInput.value = selectedSuggestion.textContent;
        suggestionsList.innerHTML = '';
        selectedIndex = -1;
    }
}